package com.yungu.swift.common.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.common.dao.CommonStarCountMapper;
import com.yungu.swift.common.model.dto.CommonStarCountDto;
import com.yungu.swift.common.service.CommonStarCountService;
import com.yungu.swift.constants.DriverConstant;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class CommonStarCountServiceImpl extends BaseServiceImpl<CommonStarCountDto> implements CommonStarCountService {


    @Autowired
    private CommonStarCountMapper commonStarCountMapper;

    @Override
    protected IMapper<CommonStarCountDto> getMapper() {
        return commonStarCountMapper;
    }

    @Override
    public ResponseData<Boolean> updStarCount(String driverUuid, Double score) {
        CommonStarCountDto commonStarCountDto;
        List<CommonStarCountDto> commonStarCountDtoList = commonStarCountMapper.list(MapUtils.build("driverUuid", driverUuid));
        if (CollectionUtils.isNotEmpty(commonStarCountDtoList)) {
            commonStarCountDto = commonStarCountDtoList.get(0);
            commonStarCountDto.setUpdateOn(new Date());
        } else {
            commonStarCountDto = new CommonStarCountDto();
            commonStarCountDto.setDriverUuid(driverUuid);
            commonStarCountDto.setCreateOn(new Date());
        }
        if (DriverConstant.DRIVER_START_COUNT_ONE.compareTo(score) == 0) {
            commonStarCountDto.setStarOne(ParamUtil.defaultIfNull(commonStarCountDto.getStarOne(), 0) + 1);
        }
        if (DriverConstant.DRIVER_START_COUNT_TWO.compareTo(score) == 0) {
            commonStarCountDto.setStarTwo(ParamUtil.defaultIfNull(commonStarCountDto.getStarTwo(), 0) + 1);
        }
        if (DriverConstant.DRIVER_START_COUNT_THREE.compareTo(score) == 0) {
            commonStarCountDto.setStarThree(ParamUtil.defaultIfNull(commonStarCountDto.getStarThree(), 0) + 1);
        }
        if (DriverConstant.DRIVER_START_COUNT_FOUR.compareTo(score) == 0) {
            commonStarCountDto.setStarFour(ParamUtil.defaultIfNull(commonStarCountDto.getStarFour(), 0) + 1);
        }
        if (DriverConstant.DRIVER_START_COUNT_FIVE.compareTo(score) == 0) {
            commonStarCountDto.setStarFive(ParamUtil.defaultIfNull(commonStarCountDto.getStarFive(), 0) + 1);
        }
        if (StringUtils.isNotEmpty(commonStarCountDto.getUuid())) {
            return ResponseData.buildSuccessResponse("统计星级成功", commonStarCountMapper.edit(commonStarCountDto) > 0);
        } else {
            commonStarCountDto.setUuid(StringUtils.buildUUID());
            return ResponseData.buildSuccessResponse("统计星级成功", commonStarCountMapper.add(commonStarCountDto) > 0);
        }

    }
}